package bog

class Question {
    /*************************************/
    /** relationships                   **/
    /*************************************/
    static belongsTo = [
            owner: User,
            thread: Thread
    ]

    static hasMany = [
            upVoters: Long,
            downVoters: Long
    ]
    /*************************************/

    String title
    String content
    Calendar creationDate
    int points

    static constraints = {
        title(blank: false)
        content(blank: false)
    }

    public void upVote(Long userId){
        if (!upVoters.contains(userId) && !downVoters.contains(userId)) {
            points += 1
            addToUpVoters(userId)
        } else if (downVoters.contains(userId)) {
            removeFromDownVoters(userId)
            points += 1
        }

    }

    public void downVote(Long userId){
        if (!upVoters.contains(userId) && !downVoters.contains(userId)) {
            points -= 1
            addToDownVoters(userId)
        } else if (upVoters.contains(userId)) {
            removeFromUpVoters(userId)
            points -= 1
        }

    }

    @Override
    public java.lang.String toString() {
        return "Question{" +
                //"thread=" + thread +
                ", owner=" + owner +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", creationDate=" + creationDate +
                ", points=" + points +
                '}';
    }
}
